package com.hushow.plugin.brick.service.impl;


import com.hushow.plugin.brick.service.PaymentPlugin;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

@Service
public class PaymentService {

    public String makePayment(Map<String, Object> orderInfo, String paymentMethod) {

        //此处可以缓存
        ServiceLoader<PaymentPlugin> serviceLoader = ServiceLoader.load(PaymentPlugin.class);
        Iterator<PaymentPlugin> iterator = serviceLoader.iterator();
        Map<String, Object> pamras = new HashMap();
        pamras.put("aa", "aaa");
        while (iterator.hasNext()){
            PaymentPlugin payPlugin = iterator.next();
            if(paymentMethod.equals(payPlugin.getName())) {
                return payPlugin.pay(pamras);
            }
        }

        throw new IllegalArgumentException("Unsupported payment method: " + paymentMethod);
    }
}
